package spitter.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import domain.Spitter;
import spitter.dao.SpitterRepository;

import javax.validation.Valid;
import java.io.IOException;

@Controller
@RequestMapping("/spitter")
public class SpitterController {

    @Autowired
    private SpitterRepository spitterRepository;

//    @RequestMapping(value = "/", method = RequestMethod.GET)
//    public ModelAndView home() {
//        return new ModelAndView("home");
//    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String spitter() {
        return "registerForm";
    }

//    @RequestMapping(value = "/register", method = RequestMethod.POST)
//    public String processRegistration(@Valid domain.Spitter spitter, Errors errors,
////                                      @RequestPart("profilePicture") byte[] profilePicture
//                                        MultipartFile file
//    ) throws IOException {
////        if (errors.hasErrors()) {
////            return "registerForm";
////        }
//        //保存到文件系统
//        File dst = new File("D://upload/data/spitter/");
//        if(!dst.exists()) {
//            dst.mkdirs();
//        }
//        File newFile = new File(dst.getPath()+File.separator+file.getOriginalFilename());
//        file.transferTo(newFile);
////        map.put(spitter.getId() + "", spitter);
//        return "redirect:/spitter/" + spitter.getId();
//    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String processRegistration(@Valid Spitter spitter
            , MultipartFile profilePicture, RedirectAttributes model) throws IOException {
        spitterRepository.save(spitter);
        model.addAttribute("userName", spitter.getUserName());
        return "redirect:/spitter/{userName}";
        //        if (errors.hasErrors()) {
//            return "registerForm";
//        }
//        //保存到文件系统
//        File dst = new File("D://upload/data/spitter/");
//        if (!dst.exists()) {
//            dst.mkdirs();
//        }
//        File newFile = new File(dst.getPath() + File.separator + profilePicture.getOriginalFilename());
//        profilePicture.transferTo(newFile);
//        //把spitter存到map中
//        String idStr = (int) (Math.random() * 10000) + "";
//        spitter.setId(new Integer(idStr));
//        map.put(spitter.getId(), spitter);
//        model.addAttribute("id", spitter.getId());
//        model.addAttribute("username", spitter.getUserName());
//        model.addFlashAttribute("spitter", spitter);
//        //重定向
//        return "redirect:/spitter/{id}";
    }

//    @RequestMapping(value = "save", method = RequestMethod.GET)
//    public String saveSpittle() {
//        //save spittles.....
//        if (true) {
//            throw new DuplicateSpittleException();
//        }
//        return "redirect:/spittles";
//    }

    @RequestMapping("/{userName}")
    public String spitter(@PathVariable String userName, Model model) {
        Spitter spitter = spitterRepository.findByUserName(userName);
        model.addAttribute("spitter", spitter);
        return "spitterInfo";
    }
}
